define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'free/coupon/index' + location.search,
                    add_url: 'free/coupon/add',
                    edit_url: 'free/coupon/edit',
                    del_url: 'free/coupon/del',
                    multi_url: 'free/coupon/multi',
                    import_url: 'free/coupon/import',
                    table: 'free_coupon',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_id', title: __('Order_id'), formatter: function (value, row, index) {
                                if(value){
                                    return value;
                                }else {
                                    return '未使用';
                                }

                            }
                        },
                        {field: 'source_type', title: __('Source_type'), searchList: {"seckill":__('Seckill'),"register":__('Register'),"group":__('Join_group')}, formatter: function (value, row, index) {
                                if(value == 'seckill'){
                                    return '限时';
                                }else if(value == 'register'){
                                    return '新用户';
                                }else if(value == 'group'){
                                    return '加群';
                                }
                            }
                        },
                        {field: 'user_id', title: __('User_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'expiration_time', title: __('Expiration_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"created":__('Created'),"paid":__('Paid'),"expired":__('Expired')}, formatter: function (value){
                                if(value == 'created'){
                                    return '未使用';
                                }else if(value == 'paid'){
                                    return '已使用';
                                }else if(value == 'expired'){
                                    return '已失效';
                                }
                            }
                        },
                        {field: 'user.username', title: __('User.username'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
